package com.anders.springcloud.controller;

import com.anders.springcloud.pojo.Dept;
import com.anders.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept) {
        return deptService.addDept(dept);
    }

    @HystrixCommand(fallbackMethod = "queryByIdFallBack")
    @GetMapping("/dept/get/{id}")
    public Dept queryById(@PathVariable("id") Long id) {
        Dept dept = deptService.queryById(id);
        if (dept == null) {
            throw new RuntimeException("数据不存在");
        }
        return dept;
    }

    public Dept queryByIdFallBack(Long id) {
        return new Dept()
                .setDeptno(0)
                .setDbSource("null")
                .setName("null");
    }

    @GetMapping("/dept/all")
    public List<Dept> queryAll() {
        return deptService.queryAll();
    }
}
